<template>
  <div>
    <!-- vant 输入框 -->
    <van-search
      v-model.trim="value"
      placeholder="请输入搜索关键词"
      shape="round"
      @clear="clearFn"
      @input="inputFn"
    />

    <!-- 搜索下容器 -->
    <div v-if="resultList.length === 0" class="search_wrap">
      <!-- 标题 -->
      <p class="hot_title">热门搜索</p>
      <!-- 热搜关键词容器 -->
      <div class="hot_name_wrap">
        <!-- 每个搜索关键词 -->
        <span
          class="hot_item"
          v-for="(item, index) in hotList"
          :key="index"
          @click="clickFn(item.first)"
          >{{ item.first }}</span
        >
      </div>
    </div>

    <!-- 搜索结果下容器 -->
    <div v-else class="search_wrap">
      <!-- 标题 -->
      <p class="hot_title">最佳搜索结果</p>
      <!-- 加载 -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <Songitem
          v-for="item in resultList"
          :key="item.id"
          :title="item.name"
          :label="item.ar[0].name"
          :id="item.id"
        ></Songitem>
      </van-list>
    </div>
  </div>
</template>
<script>
import { hotSearchList, getSearhRes } from "@/api";
import Songitem from "@/components/SongItem.vue";
export default {
  components: {
    Songitem,
  },
  data() {
    return {
      value: "",
      hotList: [], //热搜数组
      resultList: [], //搜索结果数组
      loading: false,
      finished: false,
      page: 1,
    };
  },
  async created() {
    const { data: res } = await hotSearchList({});
    this.hotList = res.result.hots;
  },
  methods: {
    //点击搜索
    async clickFn(val) {
      this.value = val;
      //每次点击的时候，将Page重置为1
      this.page = 1;
      this.finished = false;
      const res2 = await this.getList();
      this.resultList = res2.result.songs;
    },
    //清除搜索框
    clearFn() {
      this.resultList = [];
    },

    //加载
    async onLoad() {
      this.page++;
      const res3 = await this.getList();
      //如果加载完了，
      if (res3.result.songs === undefined) {
        this.finished = true;
        this.loading = false;
        return;
      }
      this.resultList = [...this.resultList, ...res3.result.songs];
      this.loading = false;
    },

    //搜索框搜索
    async inputFn() {
      if (this.value.length === 0) return;
      //每次搜索从第一页开始
      this.page = 1;
      this.finished = false;
      const res3 = await this.getList();
      if (res3.result.songs === undefined) {
        this.resultList = [];
        return;
      }
      this.resultList = res3.result.songs;
    },
    //封装获取搜索匹配结果的列表
    async getList() {
      const { data: res2 } = await getSearhRes({
        keywords: this.value,
        limit: 2,
        offset: (this.page - 1) * 2,
      });
      return res2;
    },
  },
};
</script>

<style scoped>
/* 搜索容器的样式 */
.search_wrap {
  padding: 0.266667rem;
}

/*热门搜索文字标题样式 */
.hot_title {
  font-size: 0.32rem;
  color: #666;
}

/* 热搜词_容器 */
.hot_name_wrap {
  margin: 0.266667rem 0;
}

/* 热搜词_样式 */
.hot_item {
  display: inline-block;
  height: 0.853333rem;
  margin-right: 0.213333rem;
  margin-bottom: 0.213333rem;
  padding: 0 0.373333rem;
  font-size: 0.373333rem;
  line-height: 0.853333rem;
  color: #333;
  border-color: #d3d4da;
  border-radius: 0.853333rem;
  border: 1px solid #d3d4da;
}
</style>
